package com.example.testiuh;




import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.DataSetObserver;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;

//Class chính hiện trên màn hình
public class MainActivity extends Activity{
	public static final int CTDTlist = 1;
	public static final int BusList = 2;

	public static ListView listView;
	public static WebView webview;

	private DrawerLayout mDrawerLayout;
	private ListView mDrawerList;
	private ActionBarDrawerToggle mDrawerToggle;
	private CharSequence mDrawerTitle;
	private CharSequence mTitle;
	private String[] mplanetTitles;
	private EditText etSearch;
	@SuppressLint("NewApi")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);


		// Thiet lap Navication Drawer
		mTitle = mDrawerTitle = getTitle();
		//settitle cho Drawer
		mplanetTitles = getResources().getStringArray(R.array.array_plan);
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerList = (ListView) findViewById(R.id.left_drawer);
		//set layout cho drawer
		mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadwn, Gravity.START);
		mDrawerList.setAdapter(new ArrayAdapter<String>(this,
				R.layout.drawer_list_item, mplanetTitles));
		mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
		int a=mDrawerList.getSelectedItemPosition();

		// make ActionBar in bottom and top
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);
		//make ActionBar with EditText Search
		//get Text from Edittext Search
		//set Drawer vào ActionBar
		mDrawerToggle = new ActionBarDrawerToggle(
				this,                   //host Activity 
				mDrawerLayout,          //DrawerLayout object 
				R.drawable.ic_drawer,   //nav drawer image to replace 'Up' caret 
				R.string.drawer_open,   //"open drawer" description for accessibility 
				R.string.drawer_close   //"close drawer" description for accessibility 
				)
		{
			public void onDrawerClosed(View view) {
				//getActionBar().setTitle(mTitle);
				invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
			}

			public void onDrawerOpened(View drawerView) {
				//getActionBar().setTitle(mDrawerTitle);
				invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
			}
		};
		//Xứ lý sự kiện Click cho mỗi DrawerItem
		mDrawerLayout.setDrawerListener(mDrawerToggle);

		if (savedInstanceState == null) {
			selectItem(0);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return super.onCreateOptionsMenu(menu);
	}
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
		//menu.findItem(R.id.about).setVisible(!drawerOpen);
		return super.onPrepareOptionsMenu(menu);
	}

	//Hàm bắt sự kiện Menu
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (mDrawerToggle.onOptionsItemSelected(item)) {
			return true;
		}

		return super.onOptionsItemSelected(item);
	}
	//Hàm băt sự kiện Drawer Item
	private class DrawerItemClickListener implements ListView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			switch(position){
			case 0:
				menuNiemGiam(); //gọi hàm Menulist
				mDrawerLayout.closeDrawer(mDrawerList);
				return;
			case 1:
				mDrawerLayout.closeDrawer(mDrawerList);
				menuDongHp(); //gọi hàm MenuImporan
				return;
			case 2:
				mDrawerLayout.closeDrawer(mDrawerList);
				menuXacNhanQS(); //gọi gàm MenuLearn
				return;
			case 3:
				mDrawerLayout.closeDrawer(mDrawerList);
				menuCCTinHoc(); 
				return;
			case 4:
				mDrawerLayout.closeDrawer(mDrawerList);
				menuCCAnhVan(); 
				return;

			case 5:
				mDrawerLayout.closeDrawer(mDrawerList);
				menuDongPhuc(); 
				return;
			case 6:
				menuCTDaoTao(); 
				return;
			case 7:
				menuTuyenXeBus(); 
				return;
			case 8:
				menuAbout(); 
				return;

			}
		}
	}
	//set Data when click item
	public void menuNiemGiam() {
		webview.loadUrl("file:///android_asset/overflow.htm");
		return;
	}

	public void menuDongHp() {
		// TODO Auto-generated method stub

	}
	public void menuXacNhanQS() {
		// TODO Auto-generated method stub

	}
	public void menuCCTinHoc() {
		// TODO Auto-generated method stub

	}
	public void menuCCAnhVan() {
		// TODO Auto-generated method stub

	}
	public void menuDongPhuc() {
		// TODO Auto-generated method stub

	}
	public void menuCTDaoTao() {
		Intent mIntent = new Intent(this, DataList_Activity.class);
		mIntent.putExtra("listType",CTDTlist);
		startActivity(mIntent);
	}

	public void menuTuyenXeBus() {
		Intent mIntent = new Intent(this, DataList_Activity.class);
		mIntent.putExtra("listType",BusList);
		startActivity(mIntent);
	}

	public void menuAbout() {
		// TODO Auto-generated method stub

	}
	//Xử lý sự kiện click vào Item trong Drawer
	private void selectItem(int position) {

		Fragment fragment = new GalaxyFragment();
		Bundle args = new Bundle();
		args.putInt(GalaxyFragment.ARG_GALAXY_NUMBER, position);
		fragment.setArguments(args);

		FragmentManager fragmentManager = getFragmentManager();
		fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

		mDrawerList.setItemChecked(position, true);
		setTitle(mplanetTitles[position]);
		mDrawerLayout.closeDrawer(mDrawerList);
	}
	@Override
	public void setTitle(CharSequence title) {
		mTitle = title;
		getActionBar().setTitle(mDrawerTitle);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);

		mDrawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);

		mDrawerToggle.onConfigurationChanged(newConfig);
	}
	//Class khởi tạo nền của Drawer khi khởi động phần mềm
	public static class GalaxyFragment extends Fragment {
		public static final String ARG_GALAXY_NUMBER = "galaxy_number";

		public GalaxyFragment() {

		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
			/*webview.loadUrl("file:///android_asset/exporl.html");*/
			webview=(WebView)rootView.findViewById(R.id.webView1);
			webview.loadUrl("file:///android_asset/exporl.htm");
			/*listView=(ListView) rootView.findViewById(R.id.listView1);
			String titles[] = getResources().getStringArray(R.array.array_plan);
			listView.setAdapter(new ArrayAdapter<String>(getActivity(),
					R.layout.drawer_list_item, titles));*/

			return rootView;
		}
	}
}









